package cn.lijigou.shopping;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;

import cn.lijigou.shopping.service.CrashHandler;
import cn.lijigou.shopping.util.GetSingleThreadPool;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MApplication extends Application {
	public String status;
	static MApplication mDemoApp;

	private Process proc;
	private LogOutputThread logThread;
	public static StringBuffer mLog = new StringBuffer("");
	public static String IMEI = "";
	public static String SoftWareVersion="";
	private ExecutorService executorService = GetSingleThreadPool.getInstance();
	
	public MApplication() {
		super();
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.d("thread","MApplication");
		
		String cmdLine = "logcat -v time -s AndroidRuntime ActivityManager";
        try{
          proc = Runtime.getRuntime().exec(cmdLine);
          logThread = new LogOutputThread(proc.getInputStream());
          
          executorService.execute(logThread);
        }catch(Exception e){
          e.printStackTrace();
        }
        
        TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
		IMEI = tm.getDeviceId();
		SoftWareVersion = getVersionName();
		
		
		 CrashHandler crashHandler = CrashHandler.getInstance();
		 crashHandler.init(getApplicationContext());
	}
	
    /**
     * 
     * 获取当前程序的版本号 　　
     */
    private String getVersionName(){
    	
    		String version ="";
            // 获取packagemanager的实例
            PackageManager packageManager = getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo;
			try {
				packInfo = packageManager.getPackageInfo(getPackageName(),0);
				version = packInfo.versionName;
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            return version;
    }
    
    public  class LogOutputThread extends Thread {
        private BufferedReader reader;
        private BufferedWriter writer;
        private boolean isOver = false;
        
        public LogOutputThread(InputStream in){
          try{
            reader = new BufferedReader(new InputStreamReader(in));
            
          }catch(Exception e){
          }
        }
        
        @Override
        public void run() {
        	Log.d("threadpool","LogOutputThread----run---"+Thread.currentThread().getName());
          String line = null;
          try{
            while(true){
              line = reader.readLine();
              
              if(line != null){
                mLog.append(line);
                mLog.append("\n");
              }
              if(isOver){
                break;
              }
            }
          }catch(Exception e){
            e.toString();
          }
        }

    }
}


